class_name CardUI
extends Control

signal reqarent_requested(which_card_ui: CardUI)

const BASE_STYLEBOX := preload("res://Theme/card_base_stylebox.tres")
const DRAG_STYLEBOX := preload("res://Theme/card_dragging_stylebox.tres")
const HOVER_STYLEBOX := preload("res://Theme/card_hover_stylebox.tres")

@export var card: Card: set = _set_card

@onready var panel: Panel = $Panel
@onready var cost: Label = $Cost
@onready var icon: TextureRect = $icon
@onready var drop_point_detector: Area2D = $DropPointDetector
@onready var card_state_machine: CardStateMachine = $CardStateMachine as CardStateMachine
@onready var targets: Array[Node] = []
@onready var original_index := self.get_index()

var parent: Control
var tween: Tween
var playable := true : set = _set_playable
var disabled := false


func _ready() -> void:
	Events.card_aim_started.connect(_on_card_drag_or_aiming_started)
	Events.card_drag_started.connect(_on_card_drag_or_aiming_started)
	Events.card_aim_ended.connect(_on_card_drag_or_aim_ended)
	Events.card_drag_ended.connect(_on_card_drag_or_aim_ended)
	card_state_machine.init(self)

func _input(event: InputEvent) -> void:
	var game_state_machine:StateChart = get_tree().get_first_node_in_group("gameStateMachine")
	if game_state_machine.find_child("ProcessCard").active or game_state_machine.find_child("BaseState").active:
		card_state_machine.on_input(event)
	
func animate_to_position(new_position: Vector2, duration: float) -> void:
	tween = create_tween().set_trans(Tween.TRANS_CIRC).set_ease(Tween.EASE_OUT)
	tween.tween_property(self, "global_position", new_position, duration)
	
func play() -> void:
	if not card:
		return
	card.play(targets)
	queue_free()

func _set_playable(value: bool) -> void:
	playable = value
	if not playable:
		cost.add_theme_color_override("font_color", Color.RED)
		icon.modulate = Color(1, 1, 1, 0.5)
	else: 
		cost.remove_theme_color_override("font_color")
		icon.modulate = Color(1, 1, 1, 1)
	
func _on_gui_input(event: InputEvent) -> void:
	var game_state_machine:StateChart = get_tree().get_first_node_in_group("gameStateMachine")
	if game_state_machine.find_child("ProcessCard").active or game_state_machine.find_child("BaseState").active:
		card_state_machine.on_gui_input(event)

func _on_mouse_entered() -> void:
	card_state_machine.on_mouse_entered()
	
func _on_mouse_exited() -> void:
	card_state_machine.on_mouse_exited()

func _set_card(value:Card) -> void:
	if not is_node_ready():
		await ready
	
	card = value
	icon.texture = card.icon
	cost.text = str(card.cost)
	

func on_player_mana_changed(cur_mana:int) -> void:
	print("玩家剩余mana", cur_mana)
	self.playable = cur_mana >= card.cost

func _on_drop_point_detector_area_entered(area: Area2D) -> void:
	if not targets.has(area):
		targets.append(area)


func _on_drop_point_detector_area_exited(area: Area2D) -> void:
	targets.erase(area)

func _on_card_drag_or_aiming_started(used_card: CardUI) -> void:
	if used_card == self:
		return
	disabled = true
	
func _on_card_drag_or_aim_ended(_card: CardUI) -> void:
	disabled = false
